package rad.framework.uml2.uml.edit.providers.elementItemProvider.namedElement.nameSpace.classifier.structuredClassifier.Class.behavior;

import java.util.Collection;
import java.util.List;

import org.eclipse.emf.common.notify.AdapterFactory;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.util.ResourceLocator;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.edit.provider.ComposeableAdapterFactory;
import org.eclipse.emf.edit.provider.IEditingDomainItemProvider;
import org.eclipse.emf.edit.provider.IItemLabelProvider;
import org.eclipse.emf.edit.provider.IItemPropertySource;
import org.eclipse.emf.edit.provider.IStructuredItemContentProvider;
import org.eclipse.emf.edit.provider.ITreeItemContentProvider;
import org.eclipse.emf.edit.provider.ItemPropertyDescriptor;
import org.eclipse.emf.edit.provider.ViewerNotification;
import org.eclipse.uml2.uml.Behavior;
import org.eclipse.uml2.uml.UMLPackage;

import rad.framework.uml2.uml.edit.UMLEditPlugin;
import rad.framework.uml2.uml.edit.providers.elementItemProvider.namedElement.nameSpace.classifier.structuredClassifier.Class.ClassItemProvider;

public class BehaviorItemProvider
		extends ClassItemProvider
		implements IEditingDomainItemProvider, IStructuredItemContentProvider, ITreeItemContentProvider, IItemLabelProvider, IItemPropertySource {

	public BehaviorItemProvider(AdapterFactory adapterFactory) {
		super(adapterFactory);
	}

	@Override
	public List getPropertyDescriptors(Object object) {
		if (itemPropertyDescriptors == null) {
			super.getPropertyDescriptors(object);

			addIsReentrantPropertyDescriptor(object);
			addRedefinedBehaviorPropertyDescriptor(object);
			addOwnedParameterPropertyDescriptor(object);
			addContextPropertyDescriptor(object);
			addOwnedParameterSetPropertyDescriptor(object);
			addSpecificationPropertyDescriptor(object);
		}
		return itemPropertyDescriptors;
	}

	protected void addIsReentrantPropertyDescriptor(Object object) {
		itemPropertyDescriptors.add(createItemPropertyDescriptor(((ComposeableAdapterFactory) adapterFactory).getRootAdapterFactory(), getResourceLocator(), getString("_UI_Behavior_isReentrant_feature"),
				getString("_UI_PropertyDescriptor_description", "_UI_Behavior_isReentrant_feature", "_UI_Behavior_type"),
				UMLPackage.Literals.BEHAVIOR__IS_REENTRANT, true, ItemPropertyDescriptor.BOOLEAN_VALUE_IMAGE, null, null));
	}

	protected void addRedefinedBehaviorPropertyDescriptor(Object object) {
		itemPropertyDescriptors.add(createItemPropertyDescriptor(((ComposeableAdapterFactory) adapterFactory).getRootAdapterFactory(), getResourceLocator(), getString("_UI_Behavior_redefinedBehavior_feature"),
				getString("_UI_PropertyDescriptor_description", "_UI_Behavior_redefinedBehavior_feature", "_UI_Behavior_type"),
				UMLPackage.Literals.BEHAVIOR__REDEFINED_BEHAVIOR, true, null, null, null));
	}

	protected void addOwnedParameterPropertyDescriptor(Object object) {
		itemPropertyDescriptors.add(createItemPropertyDescriptor(((ComposeableAdapterFactory) adapterFactory).getRootAdapterFactory(), getResourceLocator(), getString("_UI_Behavior_ownedParameter_feature"),
				getString("_UI_PropertyDescriptor_description", "_UI_Behavior_ownedParameter_feature", "_UI_Behavior_type"),
				UMLPackage.Literals.BEHAVIOR__OWNED_PARAMETER, true, null, null, new String[] { "org.eclipse.ui.views.properties.expert"
				}));
	}

	protected void addContextPropertyDescriptor(Object object) {
		itemPropertyDescriptors.add(createItemPropertyDescriptor(((ComposeableAdapterFactory) adapterFactory).getRootAdapterFactory(), getResourceLocator(), getString("_UI_Behavior_context_feature"),
				getString("_UI_PropertyDescriptor_description", "_UI_Behavior_context_feature", "_UI_Behavior_type"),
				UMLPackage.Literals.BEHAVIOR__CONTEXT, true, null, null, new String[] { "org.eclipse.ui.views.properties.expert"
				}));
	}

	protected void addOwnedParameterSetPropertyDescriptor(Object object) {
		itemPropertyDescriptors.add(createItemPropertyDescriptor(((ComposeableAdapterFactory) adapterFactory).getRootAdapterFactory(), getResourceLocator(), getString("_UI_Behavior_ownedParameterSet_feature"),
				getString("_UI_PropertyDescriptor_description", "_UI_Behavior_ownedParameterSet_feature", "_UI_Behavior_type"),
				UMLPackage.Literals.BEHAVIOR__OWNED_PARAMETER_SET, true, null, null, new String[] { "org.eclipse.ui.views.properties.expert"
				}));
	}

	protected void addSpecificationPropertyDescriptor(Object object) {
		itemPropertyDescriptors.add(createItemPropertyDescriptor(((ComposeableAdapterFactory) adapterFactory).getRootAdapterFactory(), getResourceLocator(), getString("_UI_Behavior_specification_feature"),
				getString("_UI_PropertyDescriptor_description", "_UI_Behavior_specification_feature", "_UI_Behavior_type"),
				UMLPackage.Literals.BEHAVIOR__SPECIFICATION, true, null, null, null));
	}

	@Override
	public Collection getChildrenFeatures(Object object) {
		if (childrenFeatures == null) {
			super.getChildrenFeatures(object);
			childrenFeatures.add(UMLPackage.Literals.BEHAVIOR__OWNED_PARAMETER);
			childrenFeatures.add(UMLPackage.Literals.BEHAVIOR__PRECONDITION);
			childrenFeatures.add(UMLPackage.Literals.BEHAVIOR__POSTCONDITION);
			childrenFeatures.add(UMLPackage.Literals.BEHAVIOR__OWNED_PARAMETER_SET);
		}
		return childrenFeatures;
	}

	@Override
	protected EStructuralFeature getChildFeature(Object object, Object child) {

		return super.getChildFeature(object, child);
	}

	@Override
	public String getText(Object object) {
		final String label = ((Behavior) object).getName();
		return label == null || label.length() == 0 ? getString("_UI_Behavior_type") :
				getString("_UI_Behavior_type") + " " + label;
	}

	@Override
	public void notifyChanged(Notification notification) {
		updateChildren(notification);

		switch (notification.getFeatureID(Behavior.class)) {
			case UMLPackage.BEHAVIOR__IS_REENTRANT:
				fireNotifyChanged(new ViewerNotification(notification, notification.getNotifier(), false, true));
				return;
			case UMLPackage.BEHAVIOR__OWNED_PARAMETER:
			case UMLPackage.BEHAVIOR__PRECONDITION:
			case UMLPackage.BEHAVIOR__POSTCONDITION:
			case UMLPackage.BEHAVIOR__OWNED_PARAMETER_SET:
				fireNotifyChanged(new ViewerNotification(notification, notification.getNotifier(), true, false));
				return;
		}
		super.notifyChanged(notification);
	}

	@Override
	protected void collectNewChildDescriptors(Collection newChildDescriptors, Object object) {
		super.collectNewChildDescriptors(newChildDescriptors, object);

	}

	@Override
	public String getCreateChildText(Object owner, Object feature, Object child, Collection selection) {
		final Object childFeature = feature;
		final Object childObject = child;

		final boolean qualify = childFeature == UMLPackage.Literals.NAMESPACE__OWNED_RULE || childFeature == UMLPackage.Literals.BEHAVIOR__PRECONDITION || childFeature == UMLPackage.Literals.BEHAVIOR__POSTCONDITION || childFeature == UMLPackage.Literals.CLASSIFIER__OWNED_USE_CASE
				|| childFeature == UMLPackage.Literals.CLASS__NESTED_CLASSIFIER || childFeature == UMLPackage.Literals.BEHAVIORED_CLASSIFIER__OWNED_BEHAVIOR;

		if (qualify) {
			return getString("_UI_CreateChild_text2",
					new Object[] { getTypeText(childObject), getFeatureText(childFeature), getTypeText(owner) });
		}
		return super.getCreateChildText(owner, feature, child, selection);
	}

	@Override
	public ResourceLocator getResourceLocator() {
		return UMLEditPlugin.INSTANCE;
	}

}
